package project.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import project.model.ARoom;
import project.model.Hero;
import project.model.MagicRoom;
import project.model.Mecung;

public class ViewHelper extends JPanel implements Observer, ActionListener{

    private Map map;
    private Mecung mecung;
    
    public ViewHelper(Mecung mecung) {
        this.mecung = mecung;
        this.mecung.addObserver(this);
        this.map = new Map(mecung);
    }
    
    public void buildView() {
    	this.setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();

		this.map.veBanDo();
		c.fill = GridBagConstraints.CENTER;
		c.weightx = 0.0;
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 0;
        this.add(map, c);
        
        
        JPanel pane = new JPanel();
		pane.setLayout(new GridBagLayout());
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;       
		c.weighty = 1.0;   
		c.anchor = GridBagConstraints.PAGE_END; 
		c.insets = new Insets(10,10,10,10);  
		c.gridx = 0;       
		c.gridwidth = 1;   
		c.gridy = 1;       
		this.add(pane, c);
		
		JButton button = new JButton();
		addButtons(pane, button);
		
		pane = new JPanel();
		pane.setLayout(new GridBagLayout());
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;       
		c.weighty = 1.0;   
		c.anchor = GridBagConstraints.PAGE_END; 
		c.insets = new Insets(10,10,10,10);  
		c.gridx = 1;       
		c.gridwidth = 2;   
		c.gridy = 1;  
		this.add(pane, c);
		
		JLabel label = new JLabel();
		label.setText(mecung.getQuestion());
        c.gridx = 0;
        c.gridy = 0;
        pane.add(label,c);
        
        JTextField textField = new JTextField(20);
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 2;
        pane.add(textField, c);
        
        button = new JButton("OK");
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 0;
        button.addActionListener(this);
        pane.add(button, c);
        
		pane.hide();
    }

    @Override
    public void update(Observable observable, Object object) {
    	if (observable == mecung)
        {
    		Hero tom = mecung.getHero(); 
			ARoom aroom = mecung.timPhong(tom.getHuongMongMuonDiDen());
	        JPanel panel = (JPanel) this.getComponent(this.getComponentCount()-1);
			if(aroom.isMagicRoom()){
				JTextField textfield = (JTextField)panel.getComponent(panel.getComponentCount()-2);
				textfield.setText("");
				panel.show();
			} else {
				panel.hide();
				mecung.xedichHero(tom.getVitriHienTai(), aroom);
			}
        }
    }
    
    public void refresh() {
    	map.removeAll();
    	map.veBanDo();
    	map.revalidate();
    	map.repaint();
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		int huong = 0;
		switch (e.getActionCommand()) {
			case "Nord":
				huong = mecung.NORTH;
				break;
			case "Ost":
				huong = mecung.EAST;
				break;
			case "Sued":
				huong = mecung.SOUTH;
				break;
			case "West":
				huong = mecung.WEST;
				break;
			case "OK":
				processMecung();
				break;
		};
		
		if(huong > 0){
			mecung.dichuyen(huong);
			refresh();
		}
	}
	
	private void processMecung() {
		Hero tom = mecung.getHero(); 
		ARoom aroom = mecung.timPhong(tom.getHuongMongMuonDiDen());
		String answer = ((MagicRoom)aroom).getMagicWord();
		JPanel panel = (JPanel) this.getComponent(this.getComponentCount()-1);
		JTextField textfield = (JTextField)panel.getComponent(panel.getComponentCount()-2);
		String user_answer = textfield.getText();
		
		if(answer.equals(user_answer)){
			mecung.xedichHero(tom.getVitriHienTai(), aroom);
			refresh();
			panel.hide();
		}
	}

	private void addButtons( JPanel pane, JButton button) {
		GridBagConstraints c = new GridBagConstraints();
		
		button = new JButton("Nord");
		c.gridx = 1;
		c.gridy = 0;
		button.addActionListener(this);
		pane.add(button, c);
		
		button = new JButton("West");
		c.gridx = 0;
		c.gridy = 1;
		button.addActionListener(this);
		pane.add(button, c);
		
		button = new JButton("Ost");
		c.gridx = 2;
		c.gridy = 1;
		button.addActionListener(this);
		pane.add(button, c);
		
		button = new JButton("Sued");
		c.gridx = 1;
		c.gridy = 2;
		button.addActionListener(this);
		pane.add(button, c);
	}
}
